package dailyExe.hashTable;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class 最长重复子序列II {
    public static void main(String[] args) {
        longestConsecutive(new int[]{100,4,200,1,3,2});
    }
    public static int longestConsecutive(int[] nums) {
        if (nums == null || nums.length == 0) return 0;

        //1.创建哈希表 并且KEY为数字 VALUE为出现的次数
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                map.put(nums[i], map.get(nums[i]) + 1);
            }else map.put(nums[i], 1);
        }

        //2.使用Tree获取的时候默认就是按照KEY进行排序的
        int maxLength = 1;
        int lastNum = map.firstKey();
        int ans = 0;

        for (Integer i : map.keySet()) {
            if (i != lastNum){
                if (i == lastNum + 1){
                    //连续的
                    maxLength++;
                }else {
                    ans = Math.max(ans,maxLength);
                    maxLength = 1;
                }
            }
            lastNum = i;
        }
        return maxLength == 0? ans : Math.max(ans,maxLength);
    }
}
